// pages/myGetBooks/myGetBooks.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    books: [
      {
        'bookName': '',
        'author': '',
        'press': '',
        'id': '',
        'phone': ''
      },
    ],
    flg: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: 'https://eskkqxyr.qcloud.la/weapp/myGettingBooks',
      header: { "Content-Type": "application/json" },
      method: 'POST',
      data: {
        openId: wx.getStorageSync('openId'),
        bookStatus: 0
      },

      success: function (res) {
        console.log(res.data)

        if (res.data.data.length == 0) {
          //如果没有搜索到书籍，设置标记不渲染书籍信息
          that.setData({
            flg: false
          })
          console.log('目前没有已获取的书籍')
        } else {
          that.setData({
            flg: true
          })

          for (var i = 0; i < res.data.data.length; i++) {
            var bookNameArray = 'books[' + i + ']' + '.bookName'
            var authorArray = 'books[' + i + ']' + '.author'
            var pressArray = 'books[' + i + ']' + '.press'
            var idArray = 'books[' + i + ']' + '.id'
            var phoneArray = 'books[' + i + ']' + '.phone'
            that.setData({
              [bookNameArray]: res.data.data[i].bookName,
              [authorArray]: res.data.data[i].author,
              [pressArray]: res.data.data[i].press,
              [idArray]: res.data.data[i].id,
              [phoneArray]: res.data.data[i].phone
            })
          }

          for (var i = 0; i < res.data.data.length; i++) {
            console.log(that.data.books[i].bookName),
              console.log(that.data.books[i].author),
              console.log(that.data.books[i].press),
              console.log(that.data.books[i].id)
            console.log(that.data.books[i].phone)
          }
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})